package com.shanghaionstar.user.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.joinme.RegisterToVisitorResponse;
import com.onstar.cn.notify.NotifyCodeReq;
import com.onstar.cn.notify.NotifyConfigRes;
import com.onstar.cn.smart.BaseResponse;
import com.shanghaionstar.R;
import com.shanghaionstar.notifysetting.service.NotifySettingService;
import com.shanghaionstar.user.iview.IChangeEmailOrPhoneView;
import com.shanghaionstar.user.service.ChangeEmailOrPhoneService;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonInputLayout;

import rx.functions.Action1;

/**
 * Created by 7828wc on 16/7/1.
 */
public class ChangeEmailOrPhonePresenter extends BasePresenter<RegisterToVisitorResponse> {
    private IChangeEmailOrPhoneView emailOrPhoneView;
    private LogManager log = LogManager.getInstance();
    private ChangeEmailOrPhoneService service;
    private NotifySettingService mSettingService;


    public ChangeEmailOrPhonePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<RegisterToVisitorResponse> view) {
        super.setView(view);
        this.emailOrPhoneView = (IChangeEmailOrPhoneView) view;
        service = new ChangeEmailOrPhoneService(emailOrPhoneView.onActivity());
        mSettingService = new NotifySettingService(emailOrPhoneView.onActivity());
        super.setServiceProvider(service);
    }

    public boolean verifyInput(CommonInputLayout inputLayout, boolean isEmail) {
        if (CommonUtil.isNull(inputLayout.getText())) {
            emailOrPhoneView.verifyResult(inputLayout, R.string.input_user_info_not_null);
            return false;
        }
        if (isEmail) {
            if (!CommonUtil.isEmail(inputLayout.getText())) {
                emailOrPhoneView.verifyResult(inputLayout, R.string.new_email_error);
                return false;
            }
        } else {
            if (!CommonUtil.isPhone(inputLayout.getText())) {
                emailOrPhoneView.verifyResult(inputLayout, R.string.new_phone_error);
                return false;
            }
        }
        emailOrPhoneView.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        return true;
    }

    public boolean verifyInputCode(CommonInputLayout inputLayout) {
        if (CommonUtil.isNull(inputLayout.getText())) {
            emailOrPhoneView.verifyResult(inputLayout, R.string.input_user_info_not_null);
            return false;
        }
        emailOrPhoneView.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        return true;
    }

    public void setUserInfo(String newEmail, String newPhoneNumber, String verifyCode) {
        service.setUserInfo(newEmail, newPhoneNumber, verifyCode);
    }

    public interface INotifyProcessType{
        int GET_CODE = 0x100;
        int VALIDATE_CODE = GET_CODE+1;
        int SET_CONFIG = GET_CODE+2;
    }

    public void getNotifyCode(NotifyCodeReq req) {
        emailOrPhoneView.setLoading(true);
        subscription = mSettingService.getNotifyCode(req)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse notifyConfigRes) {
                        emailOrPhoneView.setLoading(false);
                        emailOrPhoneView.onNotifyProcess(INotifyProcessType.GET_CODE,notifyConfigRes);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (throwable instanceof OnStarException) {
                            emailOrPhoneView.setLoading(false);
                            emailOrPhoneView.onNotifyProcess(INotifyProcessType.GET_CODE,throwable);
                        }
                    }
                });
    }

    public void validateNotifyCode(NotifyCodeReq req) {
        emailOrPhoneView.setLoading(true);
        subscription = mSettingService.validateNotifyCode(req)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse notifyConfigRes) {
                        emailOrPhoneView.setLoading(false);
                        emailOrPhoneView.onNotifyProcess(INotifyProcessType.VALIDATE_CODE,notifyConfigRes);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (throwable instanceof OnStarException) {
                            emailOrPhoneView.setLoading(false);
                            emailOrPhoneView.onNotifyProcess(INotifyProcessType.VALIDATE_CODE,throwable);
                        }
                    }
                });
    }


    public void setNotifyConfig(NotifyConfigRes req) {
        emailOrPhoneView.setLoading(true);
        subscription = mSettingService.setNotifyConfig(req)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                               @Override
                               public void call(BaseResponse baseResponse) {
                                   emailOrPhoneView.setLoading(false);
                                   emailOrPhoneView.onNotifyProcess(INotifyProcessType.SET_CONFIG,baseResponse);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                if (throwable instanceof OnStarException) {
                                    emailOrPhoneView.setLoading(false);
                                    emailOrPhoneView.onNotifyProcess(INotifyProcessType.SET_CONFIG,throwable);
                                }
                            }
                        });
    }

}
